﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SFJDManagement
{
    /// <summary>
    /// UC_StatisticsPanel.xaml 的交互逻辑
    /// </summary>
    public partial class UC_StatisticsPanel : UserControl
    {
        private DataGrid data;
        public List<string> showList = new List<string>(10);
        public StackPanel layout = new StackPanel();
        public Dictionary<string, Dictionary<string, int>> dictionaryAll = new Dictionary<string, Dictionary<string, int>>(5);

        public UC_StatisticsPanel(DataGrid dataGrid)
        {
            data = dataGrid;
            InitializeComponent();
            Ini();
        }

        public void Reflesh()
        {
            layout.Children.Clear();
            Ini();
        }

        public void ChangeVisible(string name)
        {
            if (showList.IndexOf(name) < 0)
                return;
            foreach(StackPanel c in layout.Children)
            {
                if(c.Name == name)
                {
                    c.Visibility = c.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
                }
            }
        }

        public Dictionary<string, Dictionary<string, int>> GetDictionary()
        {
            return dictionaryAll;
        }

        private void Ini()
        {
            for (int i = 0; i < data.Columns.Count; i++)
            {
                if (data.Columns[i].Visibility == Visibility.Hidden)
                    continue;
                string temp = (data.GetCell(0, i).Content as TextBlock).Text;
                if (IsShow(temp) & data.Columns[i].Visibility == Visibility.Visible)
                {
                    showList.Add(data.Columns[i].Header.ToString());
                }
            }
            if(showList != null)
            {
                AddPanel();
            }
        }

        /// <summary>
        /// 判断字符是否显示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool IsShow(string str)
        {
            int tInt;
            DateTime tDateTime;
            if (Int32.TryParse(str, out tInt) | DateTime.TryParse(str, out tDateTime))
                return false;
            if(Regex.IsMatch(str, "^\\d{2}-\\d{2}(\\s\\d{2}:\\d{2}(:\\d{2})?)?$"))
                return false;
            if (Regex.IsMatch(str, "^^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}.*$"))
                return false;
            return true;
        }

        /// <summary>
        /// 根据需要自动添加模块
        /// </summary>
        private void AddPanel()
        {
            foreach(string name in showList)
            {
                layout.Children.Add(GetPanel(name));
            }
            ScrollViewer scrollViewer = new ScrollViewer();
            scrollViewer.Content = layout;
            scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            Content = scrollViewer;
        }

        /// <summary>
        /// 生成一个统计模块
        /// </summary>
        /// <param name="name">统计模块标题</param>
        /// <returns></returns>
        private StackPanel GetPanel(string name)
        {
            StackPanel b = new StackPanel();
            b.Name = name;
            Label label1 = new Label() { Content = name + " 统计：", FontWeight = FontWeights.Bold };
            b.Children.Add(label1);
            List<string> list = new List<string>(10);
            for (int i = 0; i < data.Columns.Count; i++)
            {
                if (data.Columns[i].Header.ToString() == name)
                {
                    for(int j = 0; j < data.Items.Count; j++)
                    {
                        if (data.GetRow(j).Visibility == Visibility.Visible)
                        {
                            list.Add((data.GetCell(j, i).Content as TextBlock).Text);
                        }
                    }
                    break;
                }
            }
            if(list != null)
            {
                int total = 0;
                Dictionary<string, int> dictionary = GetStatistics(list, out total);
                dictionaryAll.Add(name, dictionary);
                DataGrid dataGrid = new DataGrid()
                {
                    Name = name,
                    ItemsSource = dictionary,
                    CanUserAddRows = false,
                    IsReadOnly = true,
                    HeadersVisibility = DataGridHeadersVisibility.Column
                };
                b.Children.Add(dataGrid);
                Label label2 = new Label() { Content = "名称共计：" + total, HorizontalAlignment = HorizontalAlignment.Right};
                b.Children.Add(label2);
            }
            return b;
        }

        /// <summary>
        /// 对数据进行统计
        /// </summary>
        /// <param name="list">需要统计的列表</param>
        /// <param name="total">返回的总计数目</param>
        /// <returns></returns>
        private Dictionary<string, int> GetStatistics(List<string> list, out int total)
        {
            total = 0;
            Dictionary<string, int> b = new Dictionary<string, int>(10);
            foreach(string str in list)
            {
                if(b.Count == 0)
                {
                    b.Add(str, 1);
                }
                else
                {
                    if (b.ContainsKey(str))
                    {
                        b[str] += 1;
                    }
                    else
                    {
                        b.Add(str, 1);
                    }
                }
            }
            foreach(string key in b.Keys)
            {
                total += 1;
            }
            return b;
        }
    }
}
